package com.example.rabbit.two;

import com.example.rabbit.util.RabbitMqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 队列模式
 * 1.自动应答
 **/
public class Work01 {

    public static void main(String[] args) throws IOException, TimeoutException {
        workQueueDefault();
    }

    public static void workQueueDefault() throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();

        DeliverCallback deliverCallback = (consumeTag, message) -> {
            System.out.println("接收消息" + consumeTag + "====" + new String(message.getBody()));
        };
        CancelCallback cancelCallback = consumeTag -> {
            System.out.println("取消" + consumeTag);
        };
        System.out.println("工作线程C2等待。。。");
        String queueName = "hello";
        channel.basicConsume(queueName, true, deliverCallback, cancelCallback);
    }

    public static void autoAsk() throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        DeliverCallback deliverCallback = (consumeTag, delivery) -> {
            System.out.println("接收消息" + consumeTag + "====" + new String(delivery.getBody()));

            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        };
        CancelCallback cancelCallback = consumeTag -> {
            System.out.println("取消" + consumeTag);
        };
        System.out.println("工作线程C2等待。。。");
        String queueName = "hello";
        boolean autoAsk = false;
        channel.basicConsume(queueName, autoAsk, deliverCallback, cancelCallback);
    }
}
